﻿using Database.Context;
using QuanLyThongTinCoSo.BaseForm;
using QuanLyThongTinCoSo.Biz;
using QuanLyThongTinCoSo.Common;
using QuanLyThongTinCoSo.Forms.CoQuanCapVanBanThamDuyet;
using QuanLyThongTinCoSo.Forms.VanBanThamDuyet;
using Model = Database.Models;

namespace QuanLyThongTinCoSo.Forms.PhieuDieuTraCoSo
{
    public partial class frmThamDuyetPCCC : frmDetail
    {
        public frmThamDuyetPCCC()
        {
            InitializeComponent();
            Text = @"Thêm Thẩm duyệt PCCC";
            drlVanBanThamDuyet.Focus();
        }

        public frmThamDuyetPCCC(int id)
        {
            InitializeComponent();
            Text = @"Sửa Thẩm duyệt PCCC";
            drlVanBanThamDuyet.Focus();
            _fromtype = Constant.TypeForm.Edit;
            _id = id;

            var biz = new ThamDuyetPCCCBiz();
            var model = biz.GetItemById(id) as Model.ThamDuyetPCCC;
            if (model == null) return;

            drlVanBanThamDuyet.SelectedValue = model.VanBanThamDuyet.Id;
            txtSoVanBan.Text = model.SoVanBan;
            dtNgayCap.Value = model.NgayCap;
            drlCoQuanCap.SelectedValue = model.CoQuanCapVanBanThamDuyet.Id;
        }

        private void frmThamDuyetPCCC_Load(object sender, System.EventArgs e)
        {
            LoadVanBanThamDuyet();
            LoadCoQuanCap();
        }

        private void LoadVanBanThamDuyet()
        {
            var biz = new VanBanThamDuyetBiz();
            drlVanBanThamDuyet.DataSource = biz.GetAll();
            drlVanBanThamDuyet.DisplayMember = "Ten";
            drlVanBanThamDuyet.ValueMember = "ID";
        }

        private void btnAddVanBanThamDuyet_Click(object sender, System.EventArgs e)
        {
            var frm = new frmVanBanThamDuyetDetail();
            frm.OnLoadData += LoadVanBanThamDuyet;
            frm.ShowDialog();
        }

        private void LoadCoQuanCap()
        {
            var biz = new CoQuanCapVanBanThamDuyetBiz();
            drlCoQuanCap.DataSource = biz.GetAll();
            drlCoQuanCap.DisplayMember = "Ten";
            drlCoQuanCap.ValueMember = "ID";
        }

        private void btnAddCoQuanCap_Click(object sender, System.EventArgs e)
        {
            var frm = new frmCoQuanCapVanBanThamDuyetDetail();
            frm.OnLoadData += LoadCoQuanCap;
            frm.ShowDialog();
        }

        #region Validation

        protected override bool Validation()
        {
            bool flag = true;

            errorProvider.Dispose();

            if (string.IsNullOrEmpty(txtSoVanBan.Text))
            {
                errorProvider.SetError(txtSoVanBan, "Bạn chưa nhập Số văn bản. Xin vui lòng kiểm tra lại");
                flag = false;
            }

            return flag;
        }

        #endregion

        #region Insert

        protected override void Insert()
        {
            var context = new Context();
            var model = new Model.ThamDuyetPCCC();

            var vanbanthamduyetBiz = new VanBanThamDuyetBiz(context);
            var vanbanthamduyetId = (int) drlVanBanThamDuyet.SelectedValue;
            model.VanBanThamDuyet = vanbanthamduyetBiz.GetItemById(vanbanthamduyetId) as Model.VanBanThamDuyetPCCC;

            model.SoVanBan = txtSoVanBan.Text.Trim();
            model.NgayCap = dtNgayCap.Value;
            model.GhiChu = txtGhiChu.Text.Trim();
            var coquancapBiz = new CoQuanCapVanBanThamDuyetBiz(context);
            var coquancapId = (int)drlCoQuanCap.SelectedValue;
            model.CoQuanCapVanBanThamDuyet = coquancapBiz.GetItemById(coquancapId) as Model.CoQuanCapVanBanThamDuyet;

            var biz = new ThamDuyetPCCCBiz(context);
            biz.Insert(model);
        }

        #endregion

        #region Edit

        protected override void Edit()
        {
            var context = new Context();
            var biz = new ThamDuyetPCCCBiz(context);
            var item = biz.GetItemById(_id) as Model.ThamDuyetPCCC;
            if (item != null)
            {
                item.SoVanBan = txtSoVanBan.Text.Trim();
                item.NgayCap = dtNgayCap.Value;
                item.GhiChu = txtGhiChu.Text.Trim();

                var vanbanthamduyetBiz = new VanBanThamDuyetBiz(context);
                var vanbanthamduyetId = (int)drlVanBanThamDuyet.SelectedValue;
                item.VanBanThamDuyet = vanbanthamduyetBiz.GetItemById(vanbanthamduyetId) as Model.VanBanThamDuyetPCCC;

                var coquancapBiz = new CoQuanCapVanBanThamDuyetBiz(context);
                var coquancapId = (int)drlCoQuanCap.SelectedValue;
                item.CoQuanCapVanBanThamDuyet = coquancapBiz.GetItemById(coquancapId) as Model.CoQuanCapVanBanThamDuyet;
                biz.Update(item);
            }
        }

        #endregion
    }
}